﻿using CPPEI.Coolzon.Core.Data;
using CPPEI.Coolzon.Core.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Core.Service
{
    /// <summary>
    /// 方案服务
    /// </summary>
    public class SolutionService
    {
        /// <summary>
        /// 添加方案操作
        /// </summary>
        /// <returns></returns>
        public ServiceResult AddSolution(Solution solution)
        {
            if (solution == null)
            {
                return null;
            }
            ServiceResult result = new ServiceResult();
            try
            {
                using (DbContext content = new DbContext())
                {
                    ISolutionRepository solutionRepository = RepositoryFactory.Create<ISolutionRepository>();
                    IPrivilegeRepository privilegeRepository = RepositoryFactory.Create<IPrivilegeRepository>();
                    
                    Privilege privilege = new Privilege();
                    privilege.Id = Guid.NewGuid();
                    privilege.Name = solution.Caption;
                    privilege.Type = "SOLUTION";
                    privilege.Code = solution.Name;

                    if (solutionRepository.CreateSolution(content, solution) && privilegeRepository.CreatePrivilege(content, privilege))
                    {
                        result.IsSuccess = true;
                        result.Message = "创建方案成功";
                    }
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "创建方案失败";
            }
            return result;
        }

        /// <summary>
        /// 分配方案操作
        /// </summary>
        /// <param name="context"></param>
        /// <param name="solutionId"></param>
        /// <param name="objectType"></param>
        /// <param name="objectId"></param>
        /// <returns></returns>
        public ServiceResult AssignSolution(DbContext context, string solutionId, string objectType, string objectId)
        {
            ServiceResult result = new ServiceResult();
            try
            {
                using (context)
                {
                    AssignedPrivilege privilege = new AssignedPrivilege();
                    privilege.Id = Guid.NewGuid();
                    privilege.ObjectId = new Guid(objectId);
                    privilege.ObjectType = objectType;
                    privilege.PrivilegeId = new Guid(solutionId);
                    IAssignedPrivilegeRepository assignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                    if (assignedPrivilegeRepository.CreateAssignedPrivilege(context, privilege))
                    {
                        result.IsSuccess = true;
                        result.Message = "分配方案成功";
                    }
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "分配方案失败";
            }
            return result;
        }

        /// <summary>
        /// 根据用户分配方案操作
        /// </summary>
        /// <param name="context"></param>
        /// <param name="solutionId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public ServiceResult AssignSolutionToUser(DbContext context, string solutionId, string userId)
        {
            ServiceResult result = new ServiceResult();
            try
            {
                using (context)
                {
                    IAssignedPrivilegeRepository iAssignedPrivilegeRepository = RepositoryFactory.Create<IAssignedPrivilegeRepository>();
                    AssignedPrivilege assignedPrivilege = new AssignedPrivilege();
                    assignedPrivilege.Id = Guid.NewGuid();
                    assignedPrivilege.PrivilegeId = new Guid(solutionId);
                    assignedPrivilege.ObjectId = new Guid(userId);
                    assignedPrivilege.ObjectType = "Solution";
                    assignedPrivilege.ObjectData = "";
                    if (iAssignedPrivilegeRepository.CreateAssignedPrivilege(context, assignedPrivilege))
                    {
                        result.IsSuccess = true;
                        result.Message = "用户分配方案成功";
                    }
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "用户分配方案失败";
            }
            return result;
        }

        /// <summary>
        /// 移除方案操作
        /// </summary>
        /// <returns></returns>
        public ServiceResult RemoveSolution(string solutionId)
        {
            ServiceResult result = new ServiceResult();
            try
            {
                using (DbContext context = new DbContext())
                {
                    ISolutionRepository solutionRepository = RepositoryFactory.Create<ISolutionRepository>();
                    IPrivilegeRepository privilegeRepository = RepositoryFactory.Create<IPrivilegeRepository>();
                    Privilege privilege = privilegeRepository.GetPrivilege(context, new Guid(solutionId));
                    Solution solution = solutionRepository.GetSolution(context, new Guid(solutionId));
                   
                    if (privilegeRepository.RemovePrivilege(context, privilege) &&
                     solutionRepository.RemoveSolution(context, solution))
                    {
                        result.IsSuccess = true;
                        result.Message = "移除方案成功";
                    }
                }
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "移除方案失败";
            }
            return result;
        }



    }
}
